//
//  PhoneInputVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/26.
//

import UIKit

class PhoneInputVC: ZMViewController {

    //MARK:--------------------属性--------------------
    // 0 验证码登录 1 密码登录
    let loginType = BehaviorRelay(value: 1)
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var phone: UITextField!
    
    @IBOutlet weak var loginBtn: UIButton!
    
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var loginTypeBtn: UIButton!
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        phone.becomeFirstResponder()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = ""
        phone.delegate = InputMananger.shard.onlyNumber
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        phone.rx.text.orEmpty.map({$0.zm.isPhoneNum()}).subscribe(onNext:{ [unowned self] resp in
            self.loginBtn.isEnabled = resp
            self.loginBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
        
        loginType.subscribe(onNext:{ [unowned self] resp in
            if resp == 0 {
                // 验证码登录
                loginTypeBtn.setTitle("密码登录", for: .normal)
                titleLabel.text = "请输入手机号"
                loginBtn.setTitle("获取验证码", for: .normal)
            } else {
                // 密码登录
                loginTypeBtn.setTitle("验证码登录", for: .normal)
                titleLabel.text = "密码登录"
                loginBtn.setTitle("下一步", for: .normal)
            }
        }).disposed(by: disposeBag)
    }
    
    // 重写返回按钮
    override func customBackButton() -> UIButton? {
        let btn = ZMUtil.button(title: "返回", image: UIImage.init(named: "back_18"), font: UIFont.systemFont(ofSize: 18), color: .black)
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        return btn
    }

    @IBAction func onPwd(_ sender: Any) {
        loginType.accept(loginType.value == 0 ? 1 : 0)
    }
    
    @IBAction func onLogin(_ sender: Any) {
        if loginType.value == 1 {
            psdLogin()
        } else {
            codeLogin()
        }
    }
    
    func codeLogin() {
        LoginService.shared.showVerifycation(vc: self) { success in
            if success {
                LoginAPI.sendCode(["phone" : self.phone.text!]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
                    if let vc = router.viewController(for: "xm://main/Login/verify_input", context: ["phone" : self.phone.text!]) {
                        self.navigationController?.pushViewController(vc, animated: true)
                    }
                }).disposed(by: self.disposeBag)
            }
        }
    }
    
    // 密码登录
    func psdLogin() {
        LoginAPI.hadRegister(["phone" : phone.text!]).rxRequest(type: [String : Any].self).subscribe(onNext:{ [unowned self] resp in
            if let ok = JSON(resp)["regState"].int {
                if ok == 1 {
                    // 注册了
                    router.push("xm://main/Login/psd_input", context: ["phone" : phone.text!])
                } else {
                    // 没有注册
                    self.codeLogin()
                }
            }
        }).disposed(by: disposeBag)
    }
    
    //MARK:--------------------delegate--------------------

}
